﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using Skywave.ComponentModel;

namespace Skywave.Web.UI.WebControls
{
    [DefaultProperty("Value")]
    [ToolboxData("<{0}:MaDateViewer runat=server></{0}:MaDateViewer>")]
    public class DateTimeViewer : WebControl
    {
        Literal dateView;

        public DateTimeViewer()
        {
            dateView = new Literal();
            dateView.Text = "-";
            this.Style.Add(HtmlTextWriterStyle.Direction, "ltr");
            this.Controls.Add(dateView);
        }

        [Bindable(true)]
        [Category("Appearance")]
        [Browsable(false)]
        public string ValueBindable
        {
            get
            {
                return mValue.ToString();
            }
            set
            {
                if (value == "")
                    Value = null;
                else
                    Value = DateTime.Parse(value);
            }
        }

        [Bindable(false)]
        [Category("Appearance")]
        public DateTime? Value
        {
            get
            {
                return mValue;
            }
            set
            {
                mValue = value;
                if (mValue == null || mValue.Value.Year == 1)
                {
                    dateView.Text = "-";
                }
                else
                {
                    DateTimeHelper tmp = new DateTimeHelper(DateTimeHelperCalendars.Persian, mValue.Value.ToLocalTime());
                    if (DisplayTime)
                        dateView.Text = tmp.ToFormatedString(DisplayFormat, TimeFormatString);
                    else
                        dateView.Text = tmp.ToFormatedString(DisplayFormat, false);
                }
            }
        }
        private DateTime? mValue = DateTime.Now;

        /// <summary>
        /// DisplayFormat Property
        /// </summary>
        [Bindable(false)]
        [Category("Appearance")]
        [Browsable(true)]
        [DefaultValue(DateTimeHelperDisplayFormats.DayOfWeekAndShortDate)]
        public DateTimeHelperDisplayFormats DisplayFormat
        {
            get
            {
                object obj1 = ViewState["DisplayFormat"];
                return (obj1 == null) ? DateTimeHelperDisplayFormats.DayOfWeekAndShortDate : (DateTimeHelperDisplayFormats)obj1;
            }
            set
            {
                ViewState["DisplayFormat"] = value;
            }
        }

        /// <summary>
        /// DisplayTime Property
        /// </summary>
        [Bindable(false)]
        [Category("Appearance")]
        [Browsable(true)]
        [DefaultValue(true)]
        public bool DisplayTime
        {
            get
            {
                object obj1 = ViewState["DisplayTime"];
                return (obj1 == null) ? true : (bool)obj1;
            }
            set
            {
                ViewState["DisplayTime"] = value;
            }
        }

        /// <summary>
        /// TimeFormatString Property
        /// </summary>
        [Bindable(false)]
        [Category("Appearance")]
        [Browsable(true)]
        [DefaultValue("HH:mm:ss")]
        public string TimeFormatString
        {
            get
            {
                object obj1 = ViewState["TimeFormatString"];
                return (obj1 == null) ? null : (string)obj1;
            }
            set
            {
                ViewState["TimeFormatString"] = value;
            }
        }

    }
}
